package com.ruoyi.studentInformation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.studentInformation.domain.classInformation;
import com.ruoyi.studentInformation.domain.deptInformation;
import com.ruoyi.studentInformation.domain.studentInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.studentInformation.mapper.StudentMapper;
import com.ruoyi.studentInformation.domain.Student;
import com.ruoyi.studentInformation.service.IStudentService;

/**
 * 学生Service业务层处理
 * 
 * @author dlh
 * @date 2024-12-05
 */
@Service
public class StudentServiceImpl implements IStudentService 
{
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<deptInformation> selectAllStudent() {
        List<Student> students = studentMapper.selectAllStudents();
        if (students == null || students.isEmpty()) {
            return new ArrayList<>();
        }

        Map<String, List<Student>> deptMap = students.stream()
                .collect(Collectors.groupingBy(Student::getDept));//根据系分了个组

        // 构建 deptInformation 列表
        List<deptInformation> deptInformations = deptMap.entrySet().stream()
                .map(entry -> {//entrySet获取所有系的键值对
                    String deptName = entry.getKey();
                    List<Student> deptStudents = entry.getValue();//找到相同系内的所有学生

                    Map<String, List<Student>> classMap = deptStudents.stream()
                            .collect(Collectors.groupingBy(Student::getClassName));//根据班级分组
                    //现在classMap中的键是班级，值是学生的信息

                    //对班级进行排序，数字小的在前面
                    List<Map.Entry<String, List<Student>>> sortedClassEntries = classMap.entrySet().stream()
                            .sorted((entry1, entry2) -> {
                                //根据班级的名字和数字进行排序
                                String class1 = entry1.getKey().replaceAll("[^\\u4e00-\\u9fa5]", "");
                                String class2 = entry2.getKey().replaceAll("[^\\u4e00-\\u9fa5]", "");
                                int ClassCompare = class1.compareTo(class2);
                                if(ClassCompare != 0) {
                                    return ClassCompare;
                                }
                                Integer num1 = Integer.valueOf(entry1.getKey().replaceAll("[^0-9]", ""));
                                Integer num2 = Integer.valueOf(entry2.getKey().replaceAll("[^0-9]", ""));
                                int IntegerCompare = Integer.compare(num1, num2);
                                return IntegerCompare;
                            })
                            .collect(Collectors.toList());

                    List<classInformation> classInformations = sortedClassEntries.stream()
                            .map(entry1 -> {//获取所有该系中所有班级的键值对
                                String className = entry1.getKey();
                                List<Student> clasStudents = entry1.getValue();//该班级中的所有学生
                                List<studentInformation> studentInfos = clasStudents.stream()
                                        .map(student -> new studentInformation(student.getStudentName() + student.getStudentId(),student.getStudentName() + student.getStudentId()))
                                        .collect(Collectors.toList());//转换成studentinformation

                                return new classInformation(className,className, studentInfos);
                            }).collect(Collectors.toList());//装换成classInformation
                    // 创建 deptInformation
                    return new deptInformation(deptName,deptName,classInformations);
                }).collect(Collectors.toList());//转换成deptInformations

        return deptInformations;
    }


    /**
     * 查询学生
     * 
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public Student selectStudentById(Integer id)
    {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 查询学生列表
     * 
     * @param student 学生
     * @return 学生
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生
     * 
     * @param student 学生
     * @return 结果
     */
    @Override
    public int insertStudent(Student student)
    {
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生
     * 
     * @param student 学生
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生
     * 
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(Integer[] ids)
    {
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生信息
     * 
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentById(Integer id)
    {
        return studentMapper.deleteStudentById(id);
    }
}
